package com.onehash.onebilling.gui;

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.io.*;





import com.onehash.onebilling.entity.*;



public class OneBillingApp {
	
boolean packFrame = false;

	    /**
	     * Construct and show the application.
	     */
	    public OneBillingApp() {
	        /* Creating main frame of application */
	        OneBillingMainFrame frame = new OneBillingMainFrame();
	        /*Set size of frame*/
	        frame.setSize((int) frame.getToolkit().getScreenSize().getWidth(),
	                      ((int) frame.getToolkit().getScreenSize().getHeight()-25));
	        /*******************************************************/

	        /* Creating Splash Window */
	        SplashWindow splashWin = new SplashWindow(frame);
	        splashWin.setIcon(new ImageIcon(SplashWindow.class.getResource("images/splash.jpg")));
	        splashWin.setVisible(true);
	        splashWin.setText("Starting Application...");
	        /*********************************************/

	        // Validate frames that have preset sizes
	        // Pack frames that have useful preferred size info, e.g. from their layout
	        if (packFrame) {
	            frame.pack();
	        } else {
	            frame.validate();
	        }
	        splashWin.setText("Creating Login System...");
	        UserRegister userRegister=null;
	        User loginUser = null;
	        try{
	        	userRegister = UserRegister.getInstance();
	        }catch(FileNotFoundException fe){
	            MessageDialog.showErrorDialog(frame, frame.getTitle(), "Login Information File was not found. Please contact System Administrator.");
	            System.exit(1);
	        }catch(IOException ie){
	            MessageDialog.showErrorDialog(frame, frame.getTitle(), "An I/O Error has occurred. Please contact System Administrator.");
	            System.exit(1);
	        }

	        splashWin.setText("Prompt for Login...");
	        OneBillingLoginDlg dlg=new OneBillingLoginDlg(frame, VersionControl.getProductName()+" - Login", true);
	        if(userRegister!=null){
	            dlg.setControl(userRegister);
	        }
	        dlg.setVisible(true);
	        loginUser = dlg.getLoginUser(); //set loginUser
	        splashWin.setText("Login Successful...");

	        splashWin.setText("Loading records...");
	        MasterRecord masterRecord=MasterRecord.getInstance();

	        try{
	            //for creating crap data
	            //DataGenerator.generateData();
	            //masterRecord.serializeAll();
	            masterRecord.deserializeAll();
	        }catch(FileNotFoundException fe){
	            MessageDialog.showErrorDialog(frame, frame.getTitle(), "System Data File was not found. Please contact System Administrator.");
	            System.exit(1);
	        }catch(IOException ie){
	            ie.printStackTrace();
	            MessageDialog.showErrorDialog(frame, frame.getTitle(), "An I/O Error has occurred. Please contact System Administrator.");
	            System.exit(1);
	        }

	        //DataGenerator.printData();

	        frame.setLoginUser(loginUser);
	        frame.center();
	        frame.setVisible(true);
	        frame.setStatusText("Ready.");

	        /* closes splash screen */
	        splashWin.setVisible(false);
	        /*****************************/
	    }
	
	
	
	
	
	

	  /**
     * Application entry point.
     *
     * @param args String[]
     */
	public static void main(String[] args) {
		 SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                try {
	                    UIManager.setLookAndFeel(UIManager.
	                                             getSystemLookAndFeelClassName());
	                } catch (Exception exception) {
	                    exception.printStackTrace();
	                }

	                new OneBillingApp();
	            }
	        });
	    

	}

}
